//
//  PasswordViewController.m
//  YYPHSpeedJump
//
//  Created by speedjump on 15/12/28.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "PasswordViewController.h"
#import "PasswordTextField.h"
@interface PasswordViewController ()<UITextFieldDelegate>
{
    PasswordTextField *oldPassword;
    
    PasswordTextField *newPassword_1;
    
    PasswordTextField *newPassword_2;
}
@property (nonatomic,strong)NSMutableString *str;
@property (nonatomic,strong)UIButton *btn;
@end

@implementation PasswordViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addSubViews];
    self.title = @"修改密码";
   
}
- (NSMutableString*)str
{
    if (!_str) {
        _str = [NSMutableString string];
        
    }
    return _str;
}
- (void)addSubViews
{
    self.view.backgroundColor =kColors(239, 239, 244, 1);
   oldPassword = [[PasswordTextField alloc]initWithFrame:CGRectMake(0, 7, kScreenWidth, 44)];
    oldPassword.placeholder =@"输入旧密码";
    oldPassword.secureTextEntry = YES;
    oldPassword.backgroundColor = [UIColor whiteColor];
    oldPassword.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:oldPassword];
    newPassword_1= [[PasswordTextField alloc]initWithFrame:CGRectMake(0, 58, kScreenWidth, 44)];
    newPassword_1.placeholder = @"输入新密码(6~20位字母或数字区分大小写)";
    newPassword_1.backgroundColor = [UIColor whiteColor];
    newPassword_1.delegate =self;
    newPassword_1.secureTextEntry = YES;
    newPassword_1.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:newPassword_1];
    newPassword_2= [[PasswordTextField alloc]initWithFrame:CGRectMake(0, 110, kScreenWidth, 44)];
    newPassword_2.placeholder = @"再次输入新密码";
    newPassword_2.backgroundColor = [UIColor whiteColor];
    newPassword_2.font = [UIFont systemFontOfSize:12];
    newPassword_2.secureTextEntry = YES;
    [self.view addSubview:newPassword_2];
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame =CGRectMake(140*kWidthScale, 170, 125*kWidthScale, 29);
    [self.btn setTitle:@"确认修改" forState:UIControlStateNormal];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    self.btn.layer.cornerRadius = 5;
     self.btn.backgroundColor = kColors(218, 166, 46, 1);
    [self.btn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    self.btn.userInteractionEnabled = NO;
    [self.view addSubview:self.btn];
}
- (void)submitAction:(UIButton *)button
{
    if ([newPassword_1.text isEqualToString:newPassword_2.text]) {
        
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kBaseUrl,kModifyPWUrl];
    
    NSDictionary *dic =@{@"u":[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"],@"phone":[[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"],@"old_password":oldPassword.text,@"new_password":newPassword_2.text};
    [NetworkHandle getInfoWithUrl:urlStr dictionary:dic success:^(NSDictionary *dictionary) {
        if ([dictionary[@"code"] isEqualToString:@"200"])  {
            [Tools showAlert:@"密码修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Tools showAlert:dictionary[@"message"]];
            });
        }
    }];
    }
    else
    {
        [Tools showAlert:@"你输入的密码不一致"];
    }
        
    

}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == newPassword_1) {
      
    if ([string  isEqual: @""] && self.str != nil) {
        self.str = (NSMutableString *)[self.str substringToIndex:[self.str length] - 1];
    } else {
        self.str = [NSMutableString stringWithFormat:@"%@%@", self.str, string];
    }
        //NSLog(@"length = %ld",self.str.length);
    if (self.str.length >= 6&&self.str.length <=20) {
        self.btn.userInteractionEnabled = YES;
           } else {
         self.btn.userInteractionEnabled = NO;
    }
    }
    // NSLog(@"%@", self.str);
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
